package com.koron.order.overhaul.notified;

import com.alibaba.fastjson.JSON;
import com.koron.order.overhaul.bean.entity.OverhaulOrderBean;
import com.koron.order.overhaul.bean.query.OverhaulOrderQuery;
import com.koron.order.overhaul.service.OverhaulOrderService;
import com.koron.order.overhaul.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author twcao
 * @title: OverhaulOrderToExecute
 * @projectName GDH_EAM_V2
 * @description: 大修工单-待执行
 * @date 2021/10/814:27
 */
@Slf4j
@Component
public class OverhaulOrderToExecute implements OverhaulOrderNotify<OverhaulOrderBean> {

    @Autowired
    private OverhaulOrderService orderService;

    @Override
    public boolean execute(SessionFactory factory, OverhaulOrderBean bean) {
        log.info("大修工单-待执行，bean = {}", JSON.toJSONString(bean));
        // 仅改变工单状态即可
        OverhaulOrderQuery query = new OverhaulOrderQuery();
        query.setId(bean.getId());
        List<OverhaulOrderBean> beans = orderService.queryList(factory, query);
        Assert.isTrue(CollectionUtils.size(beans) > 0, "工单不存在");
        //  数据库中存的工单
        OverhaulOrderBean db = beans.get(0);
        if(bean.getStatus() != null) {
            db.setStatus(bean.getStatus());
        }
        orderService.saveOrUpdate(factory, db);
        return true;
    }

    @Override
    public int getOrder() {
        return 9002;
    }

    @Override
    public String status() {
        return Constants.OverhaulStatus.TO_EXECUTE;
    }
}
